package com.youths.service.impl;

import com.youths.dao.LabelAdminMapper;
import com.youths.dao.LabelConsumerMapper;
import com.youths.domain.LabelConsumer;
import com.youths.service.RegisterService;
import com.youths.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * Created by IntelliJ IDEA.
 * User: KingRainGrey
 * Date: 2020/6/22
 */
@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired(required = false)
    LabelConsumerMapper labelConsumerMapper;

    @Autowired(required = false)
    LabelAdminMapper labelAdminMapper;

    @Override
    public int consumerRegister(String consumerPhone, String password, String consumerName, int kind, int state) {
        String passwordMD5 = MD5Util.MD5("youths" + password);
        return labelConsumerMapper.register(consumerPhone, passwordMD5, consumerName, kind, state);
    }


    @Override
    public Boolean check(String consumerPhone) {
        LabelConsumer result = labelConsumerMapper.check(consumerPhone);
        if (result != null) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public String createAdmin(String phone, String name) {
        try{
            String passwordMD5 = MD5Util.MD5("youths" + phone.substring(5,11));
            Integer result = labelAdminMapper.createAdmin(phone,name,passwordMD5);
            if(result == 1){
                return "success";
            }
            return "fail";
        }catch (Exception e){
            return "fail";
        }
    }
}
